/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Util.FiltroUtil;
import Util.PostUtil;
import Model.PostInterface;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.codehaus.jettison.json.JSONException;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

/**
 *
 * @author Radek
 */
@ViewScoped
@ManagedBean(name = "mapBean")
public class MapBean implements Serializable {

    public static ArrayList<PostInterface> post = new ArrayList();
    private List<String> selectedFilters;
    private MapModel advancedModel;
    private String image;
    private Integer cod;
    HttpSession session = (HttpSession) FacesContext
            .getCurrentInstance().getExternalContext().getSession(true);

    public Integer getCod() {
        return cod;
    }

    public void setCod(Integer cod) {
        this.cod = cod;
    }

    
    
    
    public List<String> getSelectedFilters() {
        return selectedFilters;
    }

    public void setSelectedFilters(List<String> selectedFilter) {

        this.selectedFilters = selectedFilter;


    }
    private Marker marker;

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;

    }

    public MapBean() {
         try {
            advancedModel = new DefaultMapModel();
            
            post = PostUtil.getCoordenates();

            for (int i = 0; i < post.size(); i++) {
                LatLng coord1 = new LatLng(post.get(i).getLatitude(), post.get(i).getLongitude());
                advancedModel.addOverlay(new Marker(coord1, post.get(i).getTitulo(), post.get(i).getImagem(), "images/marker"+post.get(i).getCodCategoria() +".png", post.get(i).getCod().toString()));
                //advancedModel.addOverlay(new Marker);
            }
        } catch (JSONException ex) {
            Logger.getLogger(MapBean.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    public MapModel getAdvancedModel() {
       
        return advancedModel;
    }

    public Marker getMarker() {

        return marker;
    }

    public void getValues() {
        ArrayList<PostInterface> post = new ArrayList();
        advancedModel = new DefaultMapModel();

        try {
            post = FiltroUtil.filtro(selectedFilters);
        } catch (Exception ex) {
            Logger.getLogger(MapBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (int i = 0; i < post.size(); i++) {
            LatLng coord1 = new LatLng(post.get(i).getLatitude(), post.get(i).getLongitude());
            advancedModel.addOverlay(new Marker(coord1, post.get(i).getTitulo(), post.get(i).getImagem(), "images/marker"+post.get(i).getCodCategoria() +".png", post.get(i).getCod().toString()));
        }


    }
    
    public void deletaPost() {
        PostInterface post= new PostInterface();
        post.setCod(cod);
        PostUtil.deletaPost(post);
    }

}
